//
//  IMIBaseDeviceHelper.m
//  IMIFramework
//
//  Created by Travis on 09-8-7.
//  Copyright 2009 imi.im All rights reserved.
//
#import "IMIBaseDeviceHelper.h"
#import <QuartzCore/QuartzCore.h>
#include <sys/sysctl.h>  
#include <mach/mach.h>

@implementation UIDevice(IMIBase)

+(double)availableMemory{
	vm_statistics_data_t vmStats;
	mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
	kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
	
	if(kernReturn != KERN_SUCCESS) 
		return NSNotFound;
	
	return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}

+(UIImage*)screenShot{
	CGRect screenRect = [[UIScreen mainScreen] bounds];
	
	UIGraphicsBeginImageContext(screenRect.size);
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextClearRect(ctx , screenRect);
	
	UIApplication * app = [UIApplication sharedApplication];
	UIWindow * win = [app keyWindow];
	[win.layer renderInContext:ctx];
	CGContextFlush(ctx);
	
	UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();	
	
	return img;
}

@end

